package com.cxrj.zhsq.parkingLotManage.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.parkingLotManage.dao.PropertyParkingSpaceMapper;
import com.cxrj.zhsq.parkingLotManage.entity.PropertyParkingSpace;
import com.cxrj.zhsq.parkingLotManage.service.ParkingSpaceService;
import com.cxrj.zhsq.utils.DateUtils;
@Service
public class ParkingSpaceServiceImpl implements ParkingSpaceService {
	@Resource
	private PropertyParkingSpaceMapper parkingSpaceMapper;

	@Override
	public Map<String, Object> selectParkingSpacePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		sqlMap.put("name", request.getParameter("name"));
		parkingSpaceMapper.updateStateByTime(sqlMap);
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = parkingSpaceMapper.selectByCount(sqlMap) + "";
		List<PropertyParkingSpace> list = parkingSpaceMapper.selectParkingSpacePage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteParkingSpace(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int a=parkingSpaceMapper.deleteByPrimaryKey(id);
		resultMap.put("result", 0);
		if (a>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteByIds(String[] ids) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int a=parkingSpaceMapper.deleteByIds(ids);
		resultMap.put("result", 0);
		if (a>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertParkingSpaceList(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			PropertyParkingSpace parkingSpace=new PropertyParkingSpace();
			parkingSpace.setCommunityId((String) request.getSession().getAttribute("communityId"));
			parkingSpace.setCommunityName((String) request.getSession().getAttribute("communityName"));
			parkingSpace.setrId(request.getParameter("rId"));
			parkingSpace.setSellPrice(request.getParameter("sellPrice"));
			parkingSpace.setTimeLimit(request.getParameter("timeLimit"));
			Integer startNumber=Integer.valueOf(request.getParameter("startNumber"));
			Integer endNumber=Integer.valueOf(request.getParameter("endNumber"));
			if (startNumber>0 && endNumber>0) {
				for (int i = startNumber; i <= endNumber; i++) {
					parkingSpace.setId(UUID.randomUUID().toString());
					parkingSpace.setNumber(i);
					parkingSpace.setState("1");
					parkingSpace.setCreateTime(new Date());
					parkingSpaceMapper.insertSelective(parkingSpace);
				}
			}
			resultMap.put("result", 1);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", 0);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectParkingSpaceById(String id,HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyParkingSpace parkingSpace=parkingSpaceMapper.selectByPrimaryKey(id);
		resultMap.put("parkingSpace", parkingSpace);
		return resultMap;
	}

	@Override
	public Map<String, Object> checkingParkingSpaceNumber(String number, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("number", number);
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		sqlMap.put("sId", request.getParameter("rId"));
		PropertyParkingSpace parkingSpace=parkingSpaceMapper.checkingNumber(sqlMap);
		resultMap.put("result", 0);
		if (parkingSpace==null) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertParkingSpace(PropertyParkingSpace parkingSpace, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		parkingSpace.setCommunityId((String) request.getSession().getAttribute("communityId"));
		parkingSpace.setCommunityName((String) request.getSession().getAttribute("communityName"));
		parkingSpace.setId(UUID.randomUUID().toString());
		parkingSpace.setState("1");
		parkingSpace.setCreateTime(new Date());
		int a=parkingSpaceMapper.insertSelective(parkingSpace);
		resultMap.put("result", 0);	
		if (a>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateParkingSpace(PropertyParkingSpace parkingSpace, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int a=parkingSpaceMapper.updateByPrimaryKeySelective(parkingSpace);
		resultMap.put("result", 0);	
		if (a>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateParkingSpaceState(PropertyParkingSpace parkingSpace, HttpServletRequest request) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyParkingSpace space=parkingSpaceMapper.selectByPrimaryKey(parkingSpace.getId());
		if(space.getTimeLimit()!=null) {
			if(parkingSpace.getState().equals("0")) {
				parkingSpace.setEndTime(DateUtils.getnewDate(new Date(),parkingSpace.getTimeLimit()));
			}else {
		        Integer month=Integer.valueOf(space.getTimeLimit())*12;
				parkingSpace.setEndTime(DateUtils.getnewDate(new Date(),month+""));
				
			}
		}else {
			resultMap.put("result", 2);	
			return resultMap;
		}
		int a=parkingSpaceMapper.updateByPrimaryKeySelective(parkingSpace);
		resultMap.put("result", 0);	
		if (a>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}



}
